Zoklet.net

Go Back   Zoklet.net > Technology > Technophiles and Technophiliacs > Codes of all kinds
Welcome, Dfg.
You last visited: Today at 12:10 AM
Private Messages: Unread 0, Total 801.
User CP Home IRC Chat FAQ Community Calendar New Posts Search Quick Links Log Out

Advertisement
Advertisement
No logs - Anonymous IP
Reply
 
Thread Tools Search this Thread
Old 04-14-2010, 02:59 AM   #1
Zok
Autonomato-noma-napoton
 
Zok's Avatar
 
Join Date: Dec 2006
Location: Ohio
Posts: 3,666
Thanks: 71
Thanked 1,163 Times in 409 Posts
Default Long SQL Query Killer

As a server admin, often times you will want to implement a script that will kill long SQL queries - queries that cause table locks that build up requests, eventually using up all the available connections and bring down the server.

Code:
<?php
/*
        Cutoff controls how many seconds an sql query must be running before we're allowed to kill it.
        A value of 0 will kill ALL threads.
        It's wise to set this number to the same value you use in your cron job.
*/
$cutoff = 60;

/*
        A list of thread IDs that you wish to NOT kill. This is useful if you have a mysqldump running
        of if you have a service with a persistent mysql connection running on your machine.
*/
$protected_pids = array();

/*
        To keep the script from killing processes belonging to a particular database, use this. Also
        useful if you wish to spare a service from having it's connection killed.
*/
$protected_dbs = array();

$DB_HOST = 'localhost';
$DB_USERNAME = 'root';
$DB_PASSWORD = 'yourpassword';

mysql_connect($DB_HOST, $DB_USERNAME, $DB_PASSWORD) or die('Connection could not be made');
$result = mysql_query("SHOW FULL PROCESSLIST");
        while ($row=mysql_fetch_array($result)) {
        $process_id=$row["Id"];
        if ($row["Time"] > $cutoff && !(in_array($row['db'],$protected_dbs)) && !(in_array($protected_pids,$row['Id']))) {
                $fh = fopen(getcwd().'/sqlkill_log.log','a');
                fwrite($fh,'Killed process '.$process_id.' at: '.date('H:i:s, Y-m-d',time())."\n".
                        'Query: '.$row['Info'])."\n\n";
                $sql="KILL $process_id";
                mysql_query($sql);
                fclose($fh);
        }
}
?>
Just add it to your crontab. I have mine run once ever 60 seconds.
__________________
There used to be a bunch of assholes that posted on this part of zoklet here... But we systematically removed them like you would any kind of termite or roach.

Last edited by Zok; 04-14-2010 at 03:10 AM.
Zok is offline   Reply With Quote Multi-Quote This Message Quick reply to this message Thanks
Old 04-26-2010, 05:00 AM   #2
Axiom
Duke
 
Join Date: May 2008
Posts: 5,296
Thanks: 21
Thanked 53 Times in 42 Posts
Default Re: Long SQL Query Killer

Hey zok,

Are the DB errors on zoklet being caused by these stalled processes?
Axiom is offline   Reply With Quote Multi-Quote This Message Quick reply to this message Thanks
Old 04-26-2010, 08:33 PM   #3
Zok
Autonomato-noma-napoton
 
Zok's Avatar
 
Join Date: Dec 2006
Location: Ohio
Posts: 3,666
Thanks: 71
Thanked 1,163 Times in 409 Posts
Default Re: Long SQL Query Killer

Quote:
Originally Posted by Axiom View Post
Hey zok,

Are the DB errors on zoklet being caused by these stalled processes?
Usually - sometimes it's just mysql fucking up on its own
__________________
There used to be a bunch of assholes that posted on this part of zoklet here... But we systematically removed them like you would any kind of termite or roach.
Zok is offline   Reply With Quote Multi-Quote This Message Quick reply to this message Thanks
Reply

Bookmarks

Tags
killer, long, query, sql

Quick Reply
Message:
Options


Currently Active Users Viewing This Thread: 1 (1 members and 0 guests)
Dfg

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
What are the odds of a serial killer murdering another serial killer? Actor Bat Country 11 12-15-2013 12:17 PM
A Quick Query in Regards to 4chan Uber_Apples Generally Speaking 35 03-06-2010 06:12 AM
Step into the mind of the douchebag (long, long read) Intoxicated Shaman Neurons & Needles 5 12-27-2009 03:55 AM
I am a killer Haiti's Space Agency Bat Country 3 07-17-2009 01:10 PM
QUERY ERROR: BLOB/TEXT Mccodes V.2 ApeToe Codes of all kinds 3 06-14-2009 02:07 AM


All times are GMT +5. The time now is 02:22 AM.


Hot Topics
Join our Chatroom!
Users: 8
Messages/minute: 0
Topic: "Only rule: be nice or I'll cut your fucking face off, dumbshit"
Users: 27
Messages/minute: 1.6
Topic: "http://codelove.org :: Below is above in 2 codes 1 love. :: wh..."
Users: 18
Messages/minute: 5
Topic: "http://www.literotica...."
Advertisements
Your ad could go right HERE! Contact us!

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.